//
// DotNetNuke - http://www.dotnetnuke.com
// Copyright (c) 2002-2010
// by Perpetual Motion Interactive Systems Inc. ( http://www.perpetualmotion.ca )
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
// documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
// to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions 
// of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
// CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
// DEALINGS IN THE SOFTWARE.
//

using System;
using System.Collections;
using DotNetNuke.Common.Utilities;
using DotNetNuke.Services.Mail;
using DotNetNuke.Services.Vendors;
using DotNetNuke.UI.Utilities;
namespace DotNetNuke.Modules.Admin.Vendors
{
	partial class EditAffiliate : DotNetNuke.Entities.Modules.PortalModuleBase
	{
		private int VendorId = -1;
		private int AffiliateId = -1;
		protected override void OnLoad(EventArgs e)
		{
			base.OnLoad(e);

			cmdCancel.Click += new EventHandler(cmdCancel_Click);
			cmdDelete.Click += new EventHandler(cmdDelete_Click);
			cmdSend.Click += new EventHandler(cmdSend_Click);
			cmdUpdate.Click += new EventHandler(cmdUpdate_Click);

			if ((Request.QueryString["VendorId"] != null))
			{
				VendorId = Int32.Parse(Request.QueryString["VendorId"]);
			}
			if ((Request.QueryString["AffilId"] != null))
			{
				AffiliateId = Int32.Parse(Request.QueryString["AffilId"]);
			}
			cmdStartCalendar.NavigateUrl = Common.Utilities.Calendar.InvokePopupCal(txtStartDate);
			cmdEndCalendar.NavigateUrl = Common.Utilities.Calendar.InvokePopupCal(txtEndDate);
			if (Page.IsPostBack == false)
			{
				ClientAPI.AddButtonConfirm(cmdDelete, Services.Localization.Localization.GetString("DeleteItem"));
				AffiliateController objAffiliates = new AffiliateController();
				if (AffiliateId != Null.NullInteger)
				{
					AffiliateInfo objAffiliate = objAffiliates.GetAffiliate(AffiliateId, VendorId, PortalId);
					if (objAffiliate != null)
					{
						if (!Null.IsNull(objAffiliate.StartDate))
						{
							txtStartDate.Text = objAffiliate.StartDate.ToShortDateString();
						}
						if (!Null.IsNull(objAffiliate.EndDate))
						{
							txtEndDate.Text = objAffiliate.EndDate.ToShortDateString();
						}
						txtCPC.Text = objAffiliate.CPC.ToString("#0.0####");
						txtCPA.Text = objAffiliate.CPA.ToString("#0.0####");
					}
					else
					{
						Response.Redirect(EditUrl("VendorId", VendorId.ToString()), true);
					}
				}
				else
				{
					txtCPC.Text = 0.ToString("#0.0####");
					txtCPA.Text = 0.ToString("#0.0####");
					cmdDelete.Visible = false;
				}
			}
		}
		private void cmdCancel_Click(object sender, EventArgs e)
		{
			Response.Redirect(EditUrl("VendorId", VendorId.ToString()), true);
		}
		private void cmdDelete_Click(object sender, EventArgs e)
		{
			if (AffiliateId != -1)
			{
				Services.Vendors.AffiliateController objAffiliates = new Services.Vendors.AffiliateController();
				objAffiliates.DeleteAffiliate(AffiliateId);
				Response.Redirect(EditUrl("VendorId", VendorId.ToString()), true);
			}
		}
		private void cmdSend_Click(object sender, System.EventArgs e)
		{
			VendorController objVendors = new VendorController();
			VendorInfo objVendor;
			objVendor = objVendors.GetVendor(VendorId, PortalId);
			if (objVendor != null)
			{
				if (!Null.IsNull(objVendor.Email))
				{
					ArrayList Custom = new ArrayList();
					Custom.Add(objVendor.VendorName);
					Custom.Add(Common.Globals.GetPortalDomainName(PortalSettings.PortalAlias.HTTPAlias, Request, true) + "/" + Common.Globals.glbDefaultPage + "?AffiliateId=" + VendorId.ToString());
					string errorMsg = Mail.SendMail(PortalSettings.Email, objVendor.Email, "", Services.Localization.Localization.GetSystemMessage(PortalSettings, "EMAIL_AFFILIATE_NOTIFICATION_SUBJECT"), Services.Localization.Localization.GetSystemMessage(PortalSettings, "EMAIL_AFFILIATE_NOTIFICATION_BODY", Services.Localization.Localization.GlobalResourceFile, Custom), "", "", "", "", "",
					"");
					string strMessage;
					if (String.IsNullOrEmpty(errorMsg))
					{
						strMessage = Services.Localization.Localization.GetString("NotificationSuccess", this.LocalResourceFile);
						UI.Skins.Skin.AddModuleMessage(this, strMessage, UI.Skins.Controls.ModuleMessage.ModuleMessageType.GreenSuccess);
					}
					else
					{
						strMessage = Services.Localization.Localization.GetString("NotificationFailure", this.LocalResourceFile);
						strMessage = string.Format(strMessage, errorMsg);
						UI.Skins.Skin.AddModuleMessage(this, strMessage, UI.Skins.Controls.ModuleMessage.ModuleMessageType.RedError);
					}
				}
			}
		}
		private void cmdUpdate_Click(object sender, EventArgs e)
		{
			if (Page.IsValid == true)
			{
				AffiliateInfo objAffiliate = new AffiliateInfo();
				objAffiliate.AffiliateId = AffiliateId;
				objAffiliate.VendorId = VendorId;
				if (!String.IsNullOrEmpty(txtStartDate.Text))
				{
					objAffiliate.StartDate = System.DateTime.Parse(txtStartDate.Text);
				}
				else
				{
					objAffiliate.StartDate = Null.NullDate;
				}
				if (!String.IsNullOrEmpty(txtEndDate.Text))
				{
					objAffiliate.EndDate = System.DateTime.Parse(txtEndDate.Text);
				}
				else
				{
					objAffiliate.EndDate = Null.NullDate;
				}
				objAffiliate.CPC = double.Parse(txtCPC.Text);
				objAffiliate.CPA = double.Parse(txtCPA.Text);
				Services.Vendors.AffiliateController objAffiliates = new Services.Vendors.AffiliateController();
				if (AffiliateId == -1)
				{
					objAffiliates.AddAffiliate(objAffiliate);
				}
				else
				{
					objAffiliates.UpdateAffiliate(objAffiliate);
				}
				Response.Redirect(EditUrl("VendorId", VendorId.ToString()), true);
			}
		}
		public new int PortalId
		{
			get
			{
				if (PortalSettings.ActiveTab.ParentId == PortalSettings.SuperTabId)
				{
					return -1;
				}
				else
				{
					return PortalSettings.PortalId;
				}
			}
		}

		private void InitializeComponent()
		{
		}
		protected override void OnInit(EventArgs e)
		{
			base.OnInit(e);

			InitializeComponent();
		}
	}
}
